//
// Created by ins on 2021/2/28.
//
#include "MapBean.h"
#include "MapDao.h"
#include <json.h>
#include "MapPresenter.h"
#include <string>

MapDao *mapDao;
void getMap(string data, ResData& res_data);
void updateMGtime(string data, ResData& res_data);
moduleHandler map_cmds[] = {
        {"01", getMap},
        {"02", updateMGtime}
};
void updateMGtime(string data, ResData& res_data){

    Json::Value root = Util::init_root(data);
    if(!root["mid"].isNull()){
        int mid = root["mid"].asInt();

        if(mapDao->updateMGtime(mid)){
            res_data.resp_code = ERRCD_PASS;
            res_data.resp_data = OKACKDATA;
        }else{
            res_data.resp_code = ERRCD_FALSE;
            res_data.resp_data = ERRACKDATA;
        }
    }
}

void getMap(string data, ResData& res_data){

    MapBean cxmap;
    Json::Value root = Util::init_root(data);
    
    if(!root["mid"].isNull()){
        int mid = root["mid"].asInt();

        mapDao->getMap(mid, cxmap);
        mapDao->getMapExitInfo(cxmap.getMapExits());
        if(cxmap.getId() == ""){
            res_data.resp_code = ERRCD_NOMAP;
            res_data.resp_data = ERRACKDATA;
        }else{
            res_data.resp_code = ERRCD_PASS;
            res_data.resp_data = "\"map\":" + cxmap.toString();
        }
    }
}

MapPresenter::MapPresenter() {}
MapPresenter::~MapPresenter() {}

void MapPresenter::MapMsgHandler(string cmd, string data, ResData &outData) {

    cout << "map: " << data << endl;
    int i = 0;

    if(cmd.empty() || data.empty()){
        return;
    }

    for (i = 0; !map_cmds[i].cmd.empty(); ++i)
    {
        if (cmd != map_cmds[i].cmd)
            continue;
        break;
    }

    map_cmds[i].func(data, outData);
}
